const query = require('../db/mysql')
const { success, error } = require('../model/model')
class HomeController {
    getShopList = async (ctx) => {

        const { keyword, sortBy, sortType, pageIndex, pageSize } = ctx.query;
        
        let sql = `select * from shops where 1=1 `
        let sqlCount=`select count(id) as total from shops where 1=1 `
        
        if(keyword){
            sql+=`and title like "%${keyword}%"`
            sqlCount+=`and title like "%${keyword}%"`
        }
        if(sortBy&&sortType){
            sql+=`order by ${sortBy} ${sortType}`
        }
        if(pageIndex&&pageSize){
            sql+=`limit ${(pageIndex-1)*pageSize},${pageSize}`
        }
        const data = await query(sql)
        
        const total=await query(sqlCount)
        ctx.body = success({
            data,
            total: total[0].total
        })
    }
    getShopDetail=async(ctx)=>{
        const {id}=ctx.params;
        let sql=`select * from shops where id="${id}"`
        const data=await query(sql)
        ctx.body=success(data)
    }
}
module.exports = new HomeController()